package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto) {

        int row = setmealService.add(setmealDto);

        if (row > 0) {

            return R.success("添加套餐成功!");
        }
        return R.error("添加套餐失败!");

    }

    /**
     * 分页查询套餐
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(int page, int pageSize, String name) {

        try {
            IPage<SetmealDto> dtoIPage = setmealService.findPage(page, pageSize, name);

            return R.success(dtoIPage);

        } catch (Exception e) {

            e.printStackTrace();

            return R.error("查询失败!");
        }
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R findById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {

        int row = setmealService.update(setmealDto);

        if (row > 0) {

            return R.success("修改套餐成功!");
        }
        return R.error("修改套餐失败!");

    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        int row = setmealService.delete(ids);
        if (row == -1) {
            return R.error("套餐处于起售状态,不能删除!");
        }

        if (row == 0) {
            return R.error("删除套餐失败!");
        }
        return R.success("删除套餐成功!");
    }

    /**
     * 修改套餐成起售状态
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    @PostMapping("/status/1")
    public R start(@RequestParam List<Long> ids) {

        int row = setmealService.updateStart(ids);

        if (row > 0) {

            return R.success("操作成功!");

        }

        return R.error("操作失败!");
    }

    /**
     * 修改套餐成停售状态
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "Setmeal",allEntries = true)
    @PostMapping("/status/0")
    public R stop(@RequestParam List<Long> ids) {

        int row = setmealService.updateStop(ids);

        if (row > 0) {

            return R.success("操作成功!");

        }

        return R.error("操作失败!");
    }

    /**
     * 回显数据到手机端
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Cacheable(key = "#categoryId",cacheNames = "Setmeal")
    @GetMapping("/list")
    public R list(Long categoryId, Integer status) {
        List<SetmealDto> list = setmealService.list(categoryId, status);
        System.out.println("list = " + list);
        if (list != null) {
            return R.success(list);
        }
        return R.error("连接异常");
    }
}
